package com.zzx.bms.service.impl;

import com.zzx.bms.domain.Role;
import com.zzx.bms.domain.RoleExample;
import com.zzx.bms.persistence.RoleMapper;
import com.zzx.bms.service.IRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by SamZhao on 2016/12/27.
 */
@Service(value="roleService")
public class RoleServiceImpl implements IRoleService{

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> findAllRoleList() {
        RoleExample exp = new RoleExample();
        RoleExample.Criteria crt = exp.createCriteria();
        crt.andIsDeleteEqualTo("0");
        List<Role> list = roleMapper.selectByExample(exp);
        return list;
    }

    @Override
    public int saveRole(Role nursestation) {
        int flag = 0;
        if(nursestation.getId()!=null){
            flag = roleMapper.updateByPrimaryKeySelective(nursestation);
        }
        else{
            flag = roleMapper.insertSelective(nursestation);
        }
        return flag;
    }

    @Override
    public int deleteRoleById(String id) {
        int flag = 0;
        RoleExample exp = new RoleExample();
        RoleExample.Criteria crt = exp.createCriteria();
        Role nursestation = roleMapper.selectByPrimaryKey(Integer.parseInt(id));
        if(nursestation!=null){
            nursestation.setIsDelete("1");
            flag = roleMapper.updateByPrimaryKeySelective(nursestation);
        }
        return flag;
    }

    @Override
    public int checkRole(Role nursestation) {
        RoleExample exp = new RoleExample();
        RoleExample.Criteria crt = exp.createCriteria();
        if(StringUtils.isNotEmpty(nursestation.getRoleMemo()))
            crt.andRoleMemoEqualTo(nursestation.getRoleMemo().trim());
        if(nursestation.getId()!=null)
            crt.andIdNotEqualTo(nursestation.getId());
        crt.andIsDeleteEqualTo("0");
        int flag = roleMapper.countByExample(exp);
        return flag;
    }

    @Override
    public Role findRoleById(String id) {
        Role nursestation = null;
        if(StringUtils.isNotEmpty(id))
            nursestation = roleMapper.selectByPrimaryKey(Integer.parseInt(id));
        return nursestation;
    }
}
